package com.whq.knowverywell.activity;

import java.util.ArrayList;
import java.util.List;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;

import com.whq.knowverywell.R;
import com.whq.knowverywell.adapter.ManagerAccountAdapter;
import com.whq.knowverywell.business.LoginUtil;
import com.whq.knowverywell.business.db.TblUserHandler;
import com.whq.knowverywell.business.model.User;
import com.whq.knowverywell.util.LogUtil;

/**
 * 管理帐号
 * @author wanghuaiqiang
 *
 */
public class ManagerAccountActivity extends BaseLoginActivity {
	private static final String TAG = "ManagerAccountActivity";
	
	private final List<User> mUserList = new ArrayList<User>(0);
	private ManagerAccountAdapter mManagerAccountAdapter = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
	    setTheme(R.style.Theme_Sherlock_Light_NoActionBar);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.manager_account);
		
		ImageView ivBack = (ImageView) findViewById(R.id.iv_back);
		ivBack.setOnClickListener(this);
		
		ImageView ivAddAccount = (ImageView) findViewById(R.id.iv_add_account);
		ivAddAccount.setOnClickListener(this);
		
		ListView lvAccountList = (ListView) findViewById(R.id.lv_account_list);
		
		mManagerAccountAdapter = new ManagerAccountAdapter(this, this.mUserList);
		lvAccountList.setAdapter(mManagerAccountAdapter);
		mManagerAccountAdapter.notifyDataSetChanged();
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		this.initData();
	}
	
	@Override
	public void onClick(View v) {
		super.onClick(v);
		
		switch (v.getId()) {
		case R.id.iv_back:
			this.finish();
			break;
		case R.id.iv_add_account:
			this.go2AddAccount();
			break;
		}
	}
	
	public void go2AddAccount() {
		LogUtil.i(TAG, "go2AddAccount()");
		Intent intent = new Intent(this, AddAccountActivity.class);
		startActivity(intent);
	}
	
	public void deleteOneAccount(final int userId, final String groupCode, final String userName) {
		AlertDialog.Builder builder = new Builder(this);
		builder.setMessage("确认要删除用户：" + userName + " 吗？");
		builder.setTitle("删除确认");
		builder.setPositiveButton("确认", new OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				TblUserHandler tblUserHandler = new TblUserHandler(ManagerAccountActivity.this);
				tblUserHandler.delUser(userId);
				
				initData();
				dialog.dismiss();
				
				// 如果删除的是当前登录帐号，则退出
				if ( (groupCode != null) 
						&& (userName != null) 
						&& (groupCode.equals(LoginUtil.getLoginGroupCode(ManagerAccountActivity.this)))
						&& (userName.equals(LoginUtil.getLoginUserName(ManagerAccountActivity.this)))
						) {
					logout();
				}
				
			}
		});
		builder.setNegativeButton("取消", new OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
		});
		builder.create().show();
	}
	
	private void initData() {
		this.mUserList.clear();
		
		TblUserHandler tblUserHandler = new TblUserHandler(this);
		List<User> tmpList = tblUserHandler.getAllUsers();
		if (tmpList != null) {
			LogUtil.i(TAG, "tmpList size: " + tmpList.size());
			this.mUserList.addAll(tmpList);
		}
		
//		User addUser = new User();
//		addUser.setShowAddAccountBtn(true);
//		this.mUserList.add(addUser);
		
		mManagerAccountAdapter.notifyDataSetChanged();
	}
	
}
